extends CanvasLayer

var default_control := {
	"Up" : "up",
	"Down" : "down",
	"Left" : "left",
	"Right" : "right",
	"Attack" : "f",
	"Jump" : "d",
	"Dash" : "s",
	"Shock" : "a",
	"Toggle" : "space",
	"Switch" : "q",
}

@export var custom_control := default_control

func _ready() -> void:
	var click_buttons := get_tree().get_nodes_in_group("CustomClickButton")
	for button:CustomClickButton in click_buttons:
		button.label.text = custom_control[button.name]
		button.click_only_signal.connect(_on_click_button_pressed.bind(button.name, click_buttons))
		button.set_button_key_signal.connect(on_custom_key_press.bind(button.name, click_buttons))
	pass

func _process(_delta: float) -> void:
	#if Input.is_action_pressed("left"):
		#print("press left")
	pass

func _input(event: InputEvent) -> void:
	if event is InputEventKey:
		if event.ctrl_pressed:
			InputMap.action_erase_events("left")
			event.ctrl_pressed = false
			InputMap.action_add_event("left", event)
		#print("键盘输入", event.is_pressed(),event.is_released())
		#if event.is_released():
			#print(event.as_text())
	elif event is InputEventJoypadButton or event is InputEventJoypadMotion:
		print("手柄输入")
	#print(event.as_text())

func time_func() -> void:
	print("timer func")

func _on_click_button_pressed(button_name : String, arr : Array[Node]) -> void:
	for button:CustomClickButton in arr:
		if button.name != button_name:
			button.blinking = false

func on_custom_key_press(key: String, button_name : String, arr : Array[Node]) -> void:
	for button : CustomClickButton in arr:
		if button.name == button_name:
			custom_control[button.name] = key
